//
//  HYFPopMenuView.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/4.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "HYFPopMenuView.h"

@interface HYFPopMenuView()

@property (weak, nonatomic) UIImageView *bgImageView;

@end


@implementation HYFPopMenuView

- (instancetype)initWithCustomView:(UIView *)customView{
    
    self = [super init];
    
    if (self) {
        
        self.size = [UIScreen mainScreen].bounds.size;
        
        [self addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
        
        //初始化小灰框
        UIImageView *imageView = [[UIImageView alloc]init];
        
        UIImage *image = [UIImage imageNamed:@"popover_background"];
        
        imageView.image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
        
        imageView.size = CGSizeMake(customView.width + 10, customView.height + 20);
        //添加用户点击事件
        imageView.userInteractionEnabled = YES;
        
        customView.x = 5;
        customView.y = 12;
        
        [imageView addSubview:customView];
        
        [self addSubview:imageView];
        
        self.bgImageView = imageView;
        
    }
    return self;
}


- (void)hide:(UIButton *)sender{

    [self removeFromSuperview];
}

- (void)showWithView:(UIButton *)sender{

    
    //添加到window 上
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //把 sender 的坐标转换到屏幕坐标
    CGRect rect =  [sender.superview convertRect:sender.frame toView:window];
    
    self.bgImageView.centerX = CGRectGetMidX(rect);
    self.bgImageView.y = CGRectGetMaxY(rect);
    
    [window addSubview:self];

}

@end
